package com.hsgene.user.controller.manager;

import com.hsgene.common.DeleteFlagType;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.OrderHelper;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.user.condition.OperationAccountCondition;
import com.hsgene.user.domain.GeneticTestingOperationAccount;
import com.hsgene.user.service.OperationAccountService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 运营后台用户管理Controller
 *
 * @author wangbing
 * @version 1.0, 2018/11/1
 */
@RestController
@RequestMapping("/v1.0/genetic/testing/account")
public class GeneticAccountController {

    @Resource
    private OperationAccountService operationAccountService;

    @Value("${user.password.default}")
    private String defaultUserPassword;

    /**
     * 查询运营后台用户列表
     *
     * @param realName 姓名
     * @param phone    电话号码
     * @param orderBy  排序参数，默认按照创建时间（create_date_time）排序
     * @param isAsc    是否升序（默认为false：降序）
     * @param page     页号
     * @param size     每页大小
     * @return 用户列表
     */
    @GetMapping("")
    public ApiResult<Pagination<GeneticTestingOperationAccount>> queryAccounts(
            @RequestParam(name = "realName", required = false) String realName,
            @RequestParam(name = "phone", required = false) String phone,
            @RequestParam(name = "orderBy", defaultValue = "create_date_time") String orderBy,
            @RequestParam(name = "isAsc", defaultValue = "false") Boolean isAsc,
            @RequestParam("page") int page,
            @RequestParam("size") int size) {
        OperationAccountCondition condition = new OperationAccountCondition();
        condition.setRealName(realName);
        condition.setPhone(phone);
        condition.setDeleteFlag(DeleteFlagType.NOT_DELETED.getCode());
        condition.setUserType(0);
        OrderHelper orderHelper = OrderHelper.create(orderBy, isAsc);
        LimitHelper limitHelper = LimitHelper.create(page, size);

        return operationAccountService.queryOperationAccounts(condition, orderHelper, limitHelper);
    }

    /**
     * 添加管理员用户
     *
     * @param account 用户信息
     * @return 添加结果
     */
    @PostMapping("")
    public ApiResult<NullObject> addAccount(
            @RequestBody GeneticTestingOperationAccount account) {
        return operationAccountService.addOperationAccount(account);
    }
    /**
     * 添加机构用户
     *
     * @param account 用户信息
     * @return 添加结果
     */
    @PostMapping("agency")
    public ApiResult<NullObject> addAgencyAccount(
            @RequestBody GeneticTestingOperationAccount account) {
        return operationAccountService.addAgencyAccount(account);
    }
    /**
     * 修改机构用户
     *
     * @param account 用户信息
     * @return 添加结果
     */
    @PutMapping("agency")
    public ApiResult<NullObject> updateAgencyAccount(
            @RequestBody GeneticTestingOperationAccount account) {
        return operationAccountService.updateAgencyAccount(account);
    }


    /**
     * 更新管理员用户
     *
     * @param accountId 用户id
     * @param account   用户信息
     * @return 更新结果
     */
    @PutMapping("{account_id}")
    public ApiResult<NullObject> updateAccount(
            @PathVariable("account_id") String accountId,
            @RequestBody GeneticTestingOperationAccount account) {
        account.setId(accountId);
        return operationAccountService.updateOperationAccount(account);
    }

    /**
     * 查询指定的用户信息
     *
     * @param accountId 用户id
     * @return 用户信息
     */
    @GetMapping("{account_id}")
    public ApiResult<GeneticTestingOperationAccount> findAccount(
            @PathVariable("account_id") String accountId) {
        return operationAccountService.findOperationAccount(accountId);
    }

    /**
     * 删除指定用户
     *
     * @param accountId 用户id
     * @return 删除结果
     */
    @DeleteMapping("{account_id}")
    public ApiResult<NullObject> deleteAccount(
            @PathVariable("account_id") String accountId) {
        return operationAccountService.deleteOperationAccount(accountId);
    }

    /**
     * 重置管理员密码
     *
     * @param accountId 用户id
     * @return 操作结果
     */
    @PutMapping("{account_id}/password")
    public ApiResult<NullObject> resetAccountPassword(
            @PathVariable("account_id") String accountId) {
        return operationAccountService.updateOperationAccountPwd(accountId, defaultUserPassword, false);
    }

    /**
     * 用户去重验证
     *
     * @param type          验证字段类型：1-姓名，2-电话号码
     * @param field         验证值
     * @param excludeUserId 排除校验的用户id，用于更新时排除自身
     * @return 验证结果
     */
    @GetMapping("validation")
    public ApiResult<NullObject> validateAccount(
            @RequestParam("type") Integer type,
            @RequestParam("field") String field,
            @RequestParam(name = "excludeUserId", required = false) String excludeUserId) {
        return operationAccountService.validateDuplicationAccount(type, field, excludeUserId);
    }
}
